package com.fruit.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public final class DateUtil
{
	public static final String DEFAULT_DATE_FORMAT="yyyy-MM-dd";
	
	public static final String DEFAULT_DATETIME_FORMAT="yyyy-MM-dd hh:mm:ss";
	
	public static String getFormatDate(String format)
	{
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		return sdf.format(Calendar.getInstance().getTime());
	}
	
	public static String getDefaultDate()
	{
		return getFormatDate(DEFAULT_DATE_FORMAT);
	}
	
	public static String getDefaultDateTime()
	{
		return getFormatDate(DEFAULT_DATETIME_FORMAT);
	}
	
	public static boolean isBefore(Date firstDate, Date secondDate)
	{
		return compare(firstDate, secondDate)<0?true:false;
	}
	
	public static boolean isAfter(Date firstDate, Date secondDate)
	{
		return compare(firstDate, secondDate)>0?true:false;
	}
	
	public static boolean isEqual(Date firstDate, Date secondDate)
	{
		return compare(firstDate, secondDate)==0?true:false;
	}
	
	public static int compare(Date firstDate, Date secondDate)
	{
		if(firstDate==null||secondDate==null)
			throw new NullPointerException("日期不能为空");
		
		Calendar firstCalendar,secondCalendar;
		
		firstCalendar=Calendar.getInstance();
		firstCalendar.setTime(firstDate);
		
		secondCalendar=Calendar.getInstance();
		secondCalendar.setTime(secondDate);
		
		return firstCalendar.compareTo(secondCalendar);
	}
}
